<?php
class cDataValidation
        {
        var $errors=array();
        var $validations="";
        
        function checkPattern($value, $pattern)
                {
                switch ($pattern)
                        {
                        case "Empty":
                                $value=trim($value);
                                if($value=="")
                                        return true;
                                return is_null($value);
                        case "Email":
                                return ereg('^([A-Za-z0-9_]|\\-|\\.)+@(([a-z0-9_]|\\-)+\\.)+[a-z]{2,4}$',trim($value));
                        case "Login":
                                return ereg('^[A-Za-z0-9_]{5,20}$',trim($value));
                        case "Password":
                                return ereg('^[A-Za-z0-9_]{5,15}$',trim($value));
                        case "Zero":
                                return ereg('^[-|+]{0,1}0{0,}[.]{0,1}[0]{0,}$',trim($value));
                        case "Int":
                                return ereg('^[-|+]{0,1}[0-9]{1,}$',trim($value));
                        case "Negative":
                                return ereg('^-[0-9]{0,}[.]{0,1}[0-9]{0,}$',trim($value));
                        case "Number":
                                return ereg('^[-|+]{0,1}[0-9]{0,}[.]{0,1}[0-9]{0,}$',trim($value));
                        case "StrictNumber":
                                return ereg('^[-|+]{0,1}[0-9]{0,}(\\.[0-9]{1,}){0,1}$',trim($value));
                        case "Date":
                                return ereg('^[0-9]{2}/[0-9]{2}/[0-9]{4}$',trim($value));
                        case "SqlInjection":
                                return eregi('; *(delete from )|(update .+ set .+=.+)|(drop table )',trim($value));
			default:
                                return ereg($pattern,trim($value));
                        }
                }
        
        function isDate($given_date,$date_range="1990-01-01~2099-12-31")
                {
                list($d,$m,$y)=explode('/',trim($given_date));
                $given_date="$y-$m-$d";
                list($from_date,$to_date)=explode("~",$date_range);
                if($given_date >= $from_date && $given_date <= $to_date)
                        {
                        list ($year, $month, $day) = explode ("-", $given_date);
                        return (checkdate($month, $day, $year));
                        }
                return false;
                }
                
        function validate(&$values)
                {
		foreach($this->validations as $name=>$validateFor)
                        {
			                   $val=$values[$name];
                        if ($this->checkPattern($val,"Empty") && isset($validateFor["isEmpty"]))
                                {
                                $errors=(empty($validateFor["isEmpty"]))?"Enter value":$validateFor["isEmpty"];
                                }
                        elseif (!$this->checkPattern($val, "Number") && isset($validateFor["isNumeric"]))
                                {
                                $errors=(empty($validateFor["isNumeric"]))?"Enter numbers only":$validateFor["isNumeric"];
                                }
                        elseif (!$this->checkPattern($val, "StrictNumber") && isset($validateFor["isNumericStrict"]))
                                {
                                $errors=(empty($validateFor["isNumericStrict"]))?"Enter numbers only":$validateFor["isNumericStrict"];
                                }                                
                        elseif (!$this->checkPattern($val, "Int") && isset($validateFor["isInt"]))
                                {
                                $errors=(empty($validateFor["isInt"]))?"Enter numbers without decimal places":$validateFor["isInt"];
                                }
                        elseif ($this->checkPattern($val, "Negative") && isset($validateFor["isNegative"]))
                                {
                                $errors=(empty($validateFor["isNegative"]))?"Enter positive numbers":$validateFor["isNegative"];
                                }
                        elseif ($this->checkPattern($val, "Zero") && isset($validateFor["isZero"]))
                                {
                                $errors=(empty($validateFor["isZero"]))?"Enter non zero value":$validateFor["isZero"];
                                }
                        elseif (!$this->checkPattern($val, "Email") && isset($validateFor["isEmail"]))
                                {
                                $errors=(empty($validateFor["isEmail"]))?"Enter valid email":$validateFor["isEmail"];
                                }
                        elseif (!$this->checkPattern($val, "Password") && isset($validateFor["isPassword"]))
                                {
                                $errors=(empty($validateFor["isPassword"]))?"Invalid password":$validateFor["isPassword"];
                                }
                        elseif (!$this->checkPattern($val, "Login") && isset($validateFor["isLogin"]))
                                {
                                $errors=(empty($validateFor["isLogin"]))?"Invalid login name":$validateFor["isLogin"];
                                }
                        elseif ($this->checkPattern($val, "SqlInjection") && isset($validateFor["isSqlInjection"]))
                                {
                                $errors=(empty($validateFor["isSqlInjection"]))?"Sql Injection":$validateFor["isSqlInjection"];
                                }
                        elseif ($validateFor["isPattern"] && !$this->checkPattern($val,$validateFor["isPattern"]))
                                {
                                $errors="Invalid Input";
                                }                                
                        elseif ((!$this->checkPattern($val, "Date") || !$this->isDate($val)) && isset($validateFor["isDate"]))
                                {
                                $errors=(empty($validateFor["isDate"]))?"Enter valid date":$validateFor["isDate"];
                                }
                        elseif ((!$this->checkPattern($val, "Date") || !$this->isDate($val,$_SESSION["guser_preferences"]["valid_date_range"])) && isset($validateFor["isPreferredDate"]))
                                {
                                $errors=(empty($validateFor["isPreferredDate"]))?"Enter valid date":$validateFor["isPreferredDate"];
                                }
			else
                                $errors="";
                        if($errors)
                                $this->errors[$name]="<div style=\"text-decoration:blink\" class=\"error\">".$errors."</div>";
                        //unset($this->validations[$name]);
                        }
                }
		
		
function balanceQuantity($stock_balance,$issued_balance)
	{
	if($issued_balance>$stock_balance)
	{
	$err = "Stock value lesser";
	$x ="<div style=\"text-decoration:blink\" class=\"error\">".$err."</div>";
	return $x;
	}
	}
	}
?>
